package org.lt.cj.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class ServerProperty {

    private Properties prop = null;
    private InputStream in = null;
    private OutputStream os;

    public ServerProperty() {
        initProperty();
    }

    public ServerProperty(String path) {
        try {
            prop = new Properties();
            in = new FileInputStream(path);
        } catch (FileNotFoundException ex) {
        }
    }

    private void initProperty() {
        try {
            prop = new Properties();
            in = new FileInputStream("conf/database.properties");
        } catch (FileNotFoundException ex) {
        }
    }

    private void setIs() {
        try {
            if (in == null) {
                in = new FileInputStream("conf/database.properties");
            }
        } catch (IOException ex) {
        }
    }

    private void setOs() {
        try {
            if (os == null) {
                os = new FileOutputStream("conf/database.properties");
            }
        } catch (FileNotFoundException ex) {
        }
    }

    public void setProperty(String key, String value) {
        prop.setProperty(key, value);
    }

    public void load() {
        setIs();
        try {
            prop.load(in);
        } catch (IOException ex) {
        }
    }

    public void store(String comments) {
        try {
            setOs();
            prop.store(os, comments);
        } catch (IOException ex) {
        }
    }

    public void closeIO() {
        try {
            if (os != null) {
                os.flush();
                os.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        }
    }

    public Properties getProperty() {
        try {
            setIs();
            prop.load(in);
        } catch (IOException ex) {
        }
        return prop;
    }

    public String getValue(String key) {
        String value = "";
        try {
            setIs();
            prop.load(in);
            value = prop.getProperty(key);
        } catch (IOException ex) {
        }
        return value;
    }

    public void closeI() {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        }
    }
}
